package com.mmy.service.impl.sys;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.mmy.controller.FileNameUtils;
import com.mmy.controller.UtilConstants;
import com.mmy.dao.permission.TSysUserRoleRepository;
import com.mmy.dao.sys.TSysUserRepository;
import com.mmy.dao.sys.VSysUserRepository;
import com.mmy.domain.permissionmenu.TSysUserRole;
import com.mmy.domain.sys.TSysUser;
import com.mmy.domain.sys.VSysUser;
import com.mmy.mapper.UserDao;
import com.mmy.pojo.sys.SysUser;
import com.mmy.service.ServiceCommon;
import com.mmy.service.sys.SysUserService;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {
	@Autowired
	TSysUserRepository tSysUserRepository;
	@Autowired
	VSysUserRepository vSysUserRepository;
	@Autowired
	ServiceCommon common;
	private final ResourceLoader resourceLoader;
	@Autowired
	TSysUserRoleRepository tSysUserRoleRepository;
	@Autowired
	UserDao userDao;
	

	@Autowired
	public SysUserServiceImpl(ResourceLoader resourceLoader) {
		this.resourceLoader = resourceLoader;
	}
	
	@Value("${web.upload-path}")
	private String userPath;
	
	/**
	 * 公司审核通过,开启经理账号
	 * @param user
	 */
	public void pass(TSysUser user) {
		user.setSpare5("1");
		tSysUserRepository.save(user);
	}
	
	
	/**
	 * 查询所有用户信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:注册时搜索所有用户编号检验推荐人是否填写正确 SysUserController中 index函数
	 */
	public List<TSysUser> findAll() {
		// TODO Auto-generated method stub
		return tSysUserRepository.findAll();
	}

	/**
	 * 查询用户信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:登录时通过用户账号名或者手机号或者邮箱查询用户 SysUserController中 login函数
	 */
	public TSysUser findUser(String strAccountMobileEmail) {
		// 判断是邮箱还是手机号的正则表达式
		String strEmail = "^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$";
		String strMobile = "^[1][34578]\\d{9}$";
		TSysUser dbSysUser = new TSysUser();
		if (strAccountMobileEmail.matches(strMobile)) {
			dbSysUser = tSysUserRepository.findByUserMobile(strAccountMobileEmail);
		} else if (strAccountMobileEmail.matches(strEmail)) {
			dbSysUser = tSysUserRepository.findByEmail(strAccountMobileEmail);
		} else {
			dbSysUser = tSysUserRepository.findByUserAccount(strAccountMobileEmail);
		}
		return dbSysUser;
	}

	/**
	 * 查询用户信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:注册时查询是否已经注册 SysUserController中register 函数
	 */
	public boolean findUser(TSysUser tSysUser) {

		// 通过查询登录名、邮箱、手机号、身份证 查询是否已注册
		if (tSysUserRepository.findByUserAccount(tSysUser.getUserAccount()) == null
				&& tSysUserRepository.findByUserMobile(tSysUser.getUserMobile()) == null
				&& tSysUserRepository.findByEmail(tSysUser.getMail()) == null
				&& tSysUserRepository.findByIdentity(tSysUser.getIdentity()) == null) {
			return true;
		} else {
			return false;

		}
	}

	/**
	 * 注册用户信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:注册SysUserController中register 函数
	 */
	public void saveUser(TSysUser tSysUser, String strUpUserID, String strUserType) {
		// 判断是会员还是员工
		int iUserType = 0;
		if (strUserType == "") {
			iUserType = 0;
		} else {
			iUserType = Integer.parseInt(strUserType);
		}
		Object oUserID = null;
		if (iUserType == 0) {
			// 获得游客用户最大编号
			oUserID = tSysUserRepository.getMaxMemberUserID();
			if (oUserID == null) {
				tSysUser.setUserID(100000000);
			} else {
				tSysUser.setUserID(Integer.parseInt(oUserID.toString()) + 1);
			}
		} else if (iUserType == 1) {
			// 获得员工最大编号
			oUserID = tSysUserRepository.getMaxStaffUserID();
			if (oUserID == null) {
				tSysUser.setUserID(210000000);
			} else {
				tSysUser.setUserID(Integer.parseInt(oUserID.toString()) + 1);
			}
		} else if (iUserType == 2) {
			/* 获得员工导游最大编号 */
			oUserID = tSysUserRepository.getMaxGuideUserID();
			if (oUserID == null) {
				tSysUser.setUserID(220000000);
			} else {
				tSysUser.setUserID(Integer.parseInt(oUserID.toString()) + 1);
			}
		} else if (iUserType == 3) {
			// 获得员工司机最大编号
			oUserID = tSysUserRepository.getMaxDriverUserID();
			if (oUserID == null) {
				tSysUser.setUserID(230000000);
			} else {
				tSysUser.setUserID(Integer.parseInt(oUserID.toString()) + 1);
			}
		} else {
			// 获得员工主播最大编号
			oUserID = tSysUserRepository.getMaxAnchorUserID();
			if (oUserID == null) {
				tSysUser.setUserID(240000000);
			} else {
				tSysUser.setUserID(Integer.parseInt(oUserID.toString()) + 1);
			}
		}
		// 非游客类型注册需要审核
		if (iUserType != 0) {
			tSysUser.setMemo("未审核！");
		}
		// 在未审核通过前菜单权限与游客一致 所以暂时更改为游客类型 编号为员工编号
		iUserType = 0;
		tSysUser.setUserType((byte) iUserType);
		// 设置推荐人编码
		if (strUpUserID == "") {
			tSysUser.setUpUserID(0);
		}
		// 创建时间为当前时间
		tSysUser.setCreateDate(new Date());
		tSysUser.setCreator(tSysUser.getUserName());
		// 注册
		tSysUserRepository.save(tSysUser.getUserID());
		tSysUserRepository.save(tSysUser);
	}

	/**
	 * 获得未审核的员工信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:加载员工审核页面SysUserController中toReviewInfo()函数
	 */
	public List<VSysUser> findByMemoOrgID(int iOrgID) {
		// TODO Auto-generated method stub
		return vSysUserRepository.findByMemoOrgID(iOrgID);
	}

	/**
	 * 通过用户ID查找用户
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:选择用户SysUserController中toUpdateReview()函数
	 * 
	 *       后台修改员工
	 */
	public VSysUser findByUserID(String strUserID) {
		int iUserID = Integer.parseInt(strUserID);
		// TODO Auto-generated method stub
		return vSysUserRepository.findByUserID(iUserID);
	}

	/**
	 * 通过用户ID查找用户
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:选择用户SysUserController中updateReView()函数
	 */
	public TSysUser findByID(String strUserID) {
		// TODO Auto-generated method stub
		int iUserID = Integer.parseInt(strUserID);
		return tSysUserRepository.findByID(iUserID);
	}

	/**
	 * 修改用户
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:选择用户SysUserController中updateReview()函数
	 */
	public void save(TSysUser dbSysUser, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		if (dbSysUser.getUserID() <= 219999999) {
			dbSysUser.setUserType((byte) 1);
		} else if (dbSysUser.getUserID() <= 229999999 && dbSysUser.getUserID() >= 220000000) {
			dbSysUser.setUserType((byte) 2);
		} else if (dbSysUser.getUserID() <= 239999999 && dbSysUser.getUserID() >= 230000000) {
			dbSysUser.setUserType((byte) 3);
		} else {
			dbSysUser.setUserType((byte) 4);
		}
		dbSysUser.setMemo("通过审核！");
		dbSysUser.setModifyDate(new Date());
		dbSysUser.setReviser(sessionSysUser.getUserName());

		tSysUserRepository.save(dbSysUser);
	}

	/**
	 * 单一删除用户
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:删除用户SysUserController中toUpdateNotReview()函数
	 */
	public void delByUserID(String strUserID) {
		// TODO Auto-generated method stub
		int iUserID = Integer.parseInt(strUserID);
		tSysUserRepository.delByUserID(iUserID);
	}

	/**
	 * 创建游客
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、 用处:将未通过审核的用户创建成新游客用户SysUserController中toUpdateNotReview()函数
	 */
	public void createUser(TSysUser dbSysUser, TSysUser sessionSysUser) {
		// TODO Auto-generated method stub
		TSysUser tSysUser = new TSysUser();
		Object oUserID = null;
		// 获得游客用户最大编号
		oUserID = tSysUserRepository.getMaxMemberUserID();
		if (oUserID == null) {
			tSysUser.setUserID(100000000);
		} else {
			tSysUser.setUserID(Integer.parseInt(oUserID.toString()) + 1);
		}
		tSysUser.setUserName(dbSysUser.getUserName());
		tSysUser.setUserAccount(dbSysUser.getUserAccount());
		tSysUser.setPassWord(dbSysUser.getPassWord());
		tSysUser.setUserMobile(dbSysUser.getUserMobile());
		tSysUser.setOrgID(dbSysUser.getOrgID());
		tSysUser.setUpUserID(dbSysUser.getUpUserID());
		tSysUser.setSex(dbSysUser.getSex());
		tSysUser.setIdentity(dbSysUser.getIdentity());
		tSysUser.setTel(dbSysUser.getTel());
		tSysUser.setFax(dbSysUser.getFax());
		tSysUser.setMail(dbSysUser.getMail());
		tSysUser.setJob(dbSysUser.getJob());
		tSysUser.setCadePath(dbSysUser.getCadePath());
		tSysUser.setPhotoPath(dbSysUser.getPhotoPath());
		tSysUser.setAccountState(dbSysUser.getAccountState());
		tSysUser.setMemberState(dbSysUser.getMemberState());
		tSysUser.setBlack(dbSysUser.getBlack());
		tSysUser.setVIP(dbSysUser.getVIP());
		tSysUser.setCreateDate(dbSysUser.getCreateDate());
		tSysUser.setCreator(dbSysUser.getCreator());
		tSysUser.setUserType((byte) 0);
		tSysUser.setMemo("未通过审核！");
		tSysUser.setModifyDate(new Date());
		tSysUser.setReviser(sessionSysUser.getUserName());
		tSysUserRepository.save(tSysUser.getUserID());
		tSysUserRepository.save(tSysUser);
	}

	@Value("${web.upload-path}")
	private String path;

	/**
	 * 用户修改个人信息
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、
	 */
	public TSysUser updateUser(MultipartFile fPhotoPath, MultipartFile fCadePath, TSysUser tSysUser) {
		int iUserID = tSysUser.getUserID();
		/* 定义一个新的实体类对象 */
		TSysUser dbSysUser = tSysUserRepository.findByID(iUserID);

		// 1.定义上传的文件
		String localPath = path;
		if (fPhotoPath != null) {
			// 2.获得文件名字
			String photoName = fPhotoPath.getOriginalFilename();

			// 3.上传
			String newPhotoName = FileNameUtils.getFileName(photoName);

			// 3.1 生成新的文件名
			String realPhotoPath = localPath + "/" + newPhotoName;

			// 3.2 保存文件
			File destPhoto = new File(realPhotoPath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto.getParentFile().exists()) {
				destPhoto.getParentFile().mkdir();
			}

			try {
				fPhotoPath.transferTo(destPhoto);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dbSysUser.setPhotoPath(realPhotoPath);
		}
		if (fCadePath != null) {
			// 2.获得文件名字

			String cadeName = fCadePath.getOriginalFilename();
			// 3.上传

			String newCadeName = FileNameUtils.getFileName(cadeName);
			// 3.1 生成新的文件名

			String realCadePath = localPath + "/" + newCadeName;
			// 3.2 保存文件

			File destCade = new File(realCadePath);
			// 判断文件目目录是否存在,不存在则新建

			if (!destCade.getParentFile().exists()) {
				destCade.getParentFile().mkdir();
			}
			try {

				fCadePath.transferTo(destCade);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dbSysUser.setCadePath(realCadePath);
		}
		dbSysUser.setUserName(tSysUser.getUserName());
		dbSysUser.setIdentity(tSysUser.getIdentity());
		dbSysUser.setUserAccount(tSysUser.getUserAccount());
		dbSysUser.setUserMobile(tSysUser.getUserMobile());
		dbSysUser.setTel(tSysUser.getTel());
		dbSysUser.setMail(tSysUser.getMail());
		dbSysUser.setFax(tSysUser.getFax());
		dbSysUser.setSex(tSysUser.getSex());
		dbSysUser.setOrgID(tSysUser.getOrgID());
		dbSysUser.setJob(tSysUser.getJob());
		dbSysUser.setModifyDate(new Date());
		dbSysUser.setReviser(tSysUser.getUserName());
		tSysUserRepository.save(dbSysUser);
		return dbSysUser;
	}

	/**
	 * 用户修改个人密码
	 *
	 * @author 创建人 hanxl 整理
	 * @date 创建日期 2020.10.29
	 * 
	 *       用处：1、
	 */
	public TSysUser updatePassword(TSysUser sessionSysUser, String strPassword) {
		int iUserID = sessionSysUser.getUserID();
		/* 定义一个新的实体类对象 */
		TSysUser dbSysUser = tSysUserRepository.findByID(iUserID);
		dbSysUser.setPassWord(strPassword);
		dbSysUser.setModifyDate(new Date());
		dbSysUser.setReviser(dbSysUser.getUserName());
		tSysUserRepository.save(dbSysUser);
		return dbSysUser;
	}

	/**
	 * 获得最大编号
	 * 
	 * @return 返回值说明 最大ID
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.01.20
	 */
	public int getMaxUserID() {
		// 流水号
		Object objCode = tSysUserRepository.getMaxUserID();
		int iID = 0;
		if (objCode != null) {
			iID = Integer.parseInt(objCode.toString());
		}
		return iID;
	}

	/**
	 * 后台旅行社信息管理中显示自己公司的员工 超级管理员显示所有的员工
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @date 创建日期 2020.11.19 修改时间2021.03.17
	 */
	public List<VSysUser> findStaffByOrgID(TSysUser sessionSysUser, String strUserName, String strUserSex,
			String strUserMobile, int startIndex, int count) {
		if (strUserName == null)
			strUserName = "";
		if (strUserSex == null)
			strUserSex = "";
		if (strUserMobile == null)
			strUserMobile = "";
		int iOrgID = sessionSysUser.getOrgID();
		List<VSysUser> lstStaff = new ArrayList<VSysUser>();
		// 如果为超级管理员 显示所有的员工信息
		if (sessionSysUser.getUserID() == 0) {
			lstStaff = vSysUserRepository.findSearchALL(strUserName, strUserSex, strUserMobile, startIndex, count);
		} else {
			// 如果非超级管理员
			lstStaff = vSysUserRepository.findSearchByOrgID(iOrgID, strUserName, strUserSex, strUserMobile, startIndex,
					count);
		}
		return lstStaff;
	}

	public int queryStaffCount(TSysUser sessionSysUser, String strUserName, String strUserSex, String strUserMobile) {
		if (strUserName == null)
			strUserName = "";
		if (strUserSex == null)
			strUserSex = "";
		if (strUserMobile == null)
			strUserMobile = "";
		int iOrgID = sessionSysUser.getOrgID();
		int count = 0;
		if (sessionSysUser.getUserID() == 0) {
			count = vSysUserRepository.countALL(strUserName, strUserSex, strUserMobile);
		} else {
			count = vSysUserRepository.countByOrgID(iOrgID, strUserName, strUserSex, strUserMobile);
		}
		return count;
	}

	/**
	 * 创建员工时查询登录人所在公司的所有员工
	 * 
	 * @return 返回值说明
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public List<TSysUser> findByOrgIDandType(int iOrgID) {
		// TODO Auto-generated method stub
		return tSysUserRepository.findByOrgIDandType(iOrgID);
	}

	/**
	 * 创建员工 判断是否存在该用户
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public TSysUser findUserByIdentity(String strIdentity) {
		return tSysUserRepository.findByIdentity(strIdentity);
	}

	/**
	 * 创建员工 提交
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void createStaff(TSysUser tSysUser, MultipartFile fCadeImg, MultipartFile fPhotoImg, String[] ckone,
			TSysUser sessionSysUser) {
		/* 初始化游客账号登录名和密码 */
		tSysUser.setUserID(common.createBaseCode(UtilConstants.TableName.T_SYS_USER));
		StringBuffer staffAccount = new StringBuffer();
		staffAccount.append("staff");
		staffAccount.append(tSysUser.getUserID());
		String strStaffAccount = staffAccount.toString();
		tSysUser.setUserAccount(strStaffAccount);
		// 设置初始密码为123abc
		tSysUser.setPassWord("123abc");
		// 证件照
		// 1.定义上传的文件
		String localPath = userPath;
		if (fCadeImg != null) {
			// 2.获得文件名字
			String cadeName = fCadeImg.getOriginalFilename();

			// 3.上传
			String newCadeName = FileNameUtils.getFileName(cadeName);

			// 3.1 生成新的文件名
			String realCadePath = localPath + "/" + newCadeName;

			// 3.2 保存文件
			File destPhoto = new File(realCadePath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto.getParentFile().exists()) {
				destPhoto.getParentFile().mkdir();
			}

			try {
				fCadeImg.transferTo(destPhoto);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setCadePath(newCadeName);
		}
		// 个人照片
		if (fPhotoImg != null) {
			// 2.获得文件名字
			String photoName = fPhotoImg.getOriginalFilename();

			// 3.上传
			String newPhotoName = FileNameUtils.getFileName(photoName);

			// 3.1 生成新的文件名
			String realPhotoPath = localPath + "/" + newPhotoName;

			// 3.2 保存文件
			File destPhoto2 = new File(realPhotoPath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto2.getParentFile().exists()) {
				destPhoto2.getParentFile().mkdir();
			}

			try {
				fPhotoImg.transferTo(destPhoto2);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setPhotoPath(newPhotoName);
		}

		tSysUser.setCreateDate(new Date());
		tSysUser.setCreator(sessionSysUser.getUserName());

		/* 注册 */
		tSysUserRepository.save(tSysUser.getUserID());
		tSysUserRepository.save(tSysUser);

		/*
		 * List<Integer> iRoleIDs = new ArrayList<>();
		 */
		for (String strRoleID : ckone) {
			/* iRoleIDs.add(Integer.parseInt(strRoleID)); */
			TSysUserRole tSysUserRole = new TSysUserRole();
			int iRoleID = Integer.parseInt(strRoleID); // 获得最大用户角色对照编号
			tSysUserRole.setID(common.createMaxID(UtilConstants.TableName.T_SYS_USER_ROLE));
			tSysUserRole.setRoleID(iRoleID);
			tSysUserRole.setUserID(tSysUser.getUserID());
			tSysUserRole.setCreateDate(new Date());
			tSysUserRole.setCreator(sessionSysUser.getUserName());
			// 注册
			tSysUserRoleRepository.save(tSysUserRole.getID());
			tSysUserRoleRepository.save(tSysUserRole);
		}
	}
	
	/**
	  * @descr 自行创建旅行社时,默认创建旅行社的经理账号
	  * @param 
	  * @return 
	  * @throws 
	  * @author liys
	  * @date 2021-6-7
	 */
	public void createStaffSelf(TSysUser tSysUser) {

		tSysUser.setUserID(common.createBaseCode(UtilConstants.TableName.T_SYS_USER));
		tSysUser.setPassWord("123abc");
		tSysUser.setCreateDate(new Date());
		tSysUserRepository.save(tSysUser.getUserID());
		tSysUserRepository.save(tSysUser);
		TSysUserRole tSysUserRole = new TSysUserRole();
		int iRoleID = 1;
		tSysUserRole.setID(common.createMaxID(UtilConstants.TableName.T_SYS_USER_ROLE));
		tSysUserRole.setRoleID(iRoleID);
		tSysUserRole.setUserID(tSysUser.getUserID());
		tSysUserRole.setCreateDate(new Date());
		tSysUserRoleRepository.save(tSysUserRole.getID());
		tSysUserRoleRepository.save(tSysUserRole);
	}
	
	/**
	 * @descr 批量创建会员/员工
	 * @param
	 * @return
	 * @throws @author liys
	 * @date 2021-5-20
	 */
	public void CreateBatch(HttpSession session, List<SysUser> list) {

		TSysUser sessionUser = (TSysUser) session.getAttribute("tSysUser");
		int iUpUserID = sessionUser.getUserID();
		int iOrgID = sessionUser.getOrgID();
		for (SysUser item : list) {
			TSysUser user = new TSysUser();
			user.setUserName(item.getUserName());
			user.setSex((byte) item.getSex());
			user.setUserMobile(item.getTel());
			user.setUserType((byte) item.getUserType());
			user.setMemberState((byte) item.getMemberState());
			user.setPassWord("123abc");
			user.setOrgID(iOrgID);
			user.setUpUserID(iUpUserID);
			user.setUserID(common.createBaseCode(UtilConstants.TableName.T_SYS_USER));
			log.info(user.toString());
			tSysUserRepository.save(user.getUserID());
			tSysUserRepository.save(user);
		}
	}

	/**
	 * 修改员工提交
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void updateStaff(TSysUser tSysUser, MultipartFile fCadeImg, MultipartFile fPhotoImg, String[] ckone,
			TSysUser sessionSysUser) {
		/* 获取修改游客的ID */
		/* 定义一个新的实体类对象 */
		TSysUser dbSysUser = tSysUserRepository.findByID(tSysUser.getUserID());
		dbSysUser.setUserName(tSysUser.getUserName());
		dbSysUser.setSex(tSysUser.getSex());
		dbSysUser.setIdentity(tSysUser.getIdentity());
		dbSysUser.setUserMobile(tSysUser.getUserMobile());
		dbSysUser.setUserType(tSysUser.getUserType());
		dbSysUser.setOrgID(tSysUser.getOrgID());
		dbSysUser.setUpUserID(tSysUser.getUpUserID());
		dbSysUser.setMail(tSysUser.getMail());
		dbSysUser.setTel(tSysUser.getTel());
		dbSysUser.setFax(tSysUser.getFax());
		dbSysUser.setJob(tSysUser.getJob());

		// 1.定义上传的文件
		String localPath = userPath;
		if (fCadeImg != null) {
			// 2.获得文件名字
			String cadeName = fCadeImg.getOriginalFilename();

			// 3.上传
			String newCadeName = FileNameUtils.getFileName(cadeName);

			// 3.1 生成新的文件名
			String realCadePath = localPath + "/" + newCadeName;

			// 3.2 保存文件
			File destPhoto = new File(realCadePath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto.getParentFile().exists()) {
				destPhoto.getParentFile().mkdir();
			}

			try {
				fCadeImg.transferTo(destPhoto);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setCadePath(newCadeName);
		}
		// 个人照片
		if (fPhotoImg != null) {
			// 2.获得文件名字
			String photoName = fPhotoImg.getOriginalFilename();

			// 3.上传
			String newPhotoName = FileNameUtils.getFileName(photoName);

			// 3.1 生成新的文件名
			String realPhotoPath = localPath + "/" + newPhotoName;

			// 3.2 保存文件
			File destPhoto2 = new File(realPhotoPath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto2.getParentFile().exists()) {
				destPhoto2.getParentFile().mkdir();
			}

			try {
				fPhotoImg.transferTo(destPhoto2);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setPhotoPath(newPhotoName);
		}

		dbSysUser.setJob(tSysUser.getJob());

		dbSysUser.setVIP(tSysUser.getVIP());
		dbSysUser.setBlack(tSysUser.getBlack());
		dbSysUser.setAccountState(tSysUser.getAccountState());
		dbSysUser.setMemberState(tSysUser.getMemberState());
		dbSysUser.setMemo(tSysUser.getMemo());
		dbSysUser.setModifyDate(new Date());

		dbSysUser.setReviser(sessionSysUser.getUserName());

		tSysUserRepository.save(dbSysUser);
//删除该原有的角色
		tSysUserRoleRepository.deleteByUserID(tSysUser.getUserID());
		// 重新添加角色
		for (String strRoleID : ckone) {
			/* iRoleIDs.add(Integer.parseInt(strRoleID)); */
			TSysUserRole tSysUserRole = new TSysUserRole();
			int iRoleID = Integer.parseInt(strRoleID); // 获得最大用户角色对照编号
			tSysUserRole.setID(common.createMaxID(UtilConstants.TableName.T_SYS_USER_ROLE));
			tSysUserRole.setRoleID(iRoleID);
			tSysUserRole.setUserID(tSysUser.getUserID());
			tSysUserRole.setCreateDate(new Date());
			tSysUserRole.setCreator(sessionSysUser.getUserName());
			// 注册
			tSysUserRoleRepository.save(tSysUserRole.getID());
			tSysUserRoleRepository.save(tSysUserRole);
		}
	}
	/**
	 * 删除员工
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void deleteChecked(String checkList) {
		String[] striUserIDs = checkList.split(",");
		List<Integer> iUserIDs = new ArrayList<>();

		for (String striUserID : striUserIDs) {
			iUserIDs.add(Integer.parseInt(striUserID));
		}
		tSysUserRepository.deleteBycUserIds(iUserIDs);
	}
	/**
	 * 后台旅行社信息管理中显示自己公司的会员 超级管理员显示所有的会员
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @date 创建日期 2020.11.19 修改时间2021.03.17
	 */
	public List<VSysUser> findMemberByOrgID(TSysUser sessionSysUser, String strUserName, String strUserSex,
			String strUserMobile, int startIndex, int count) {
		if (strUserName == null)
			strUserName = "";
		if (strUserSex == null)
			strUserSex = "";
		if (strUserMobile == null)
			strUserMobile = "";
		int iOrgID = sessionSysUser.getOrgID();
		List<VSysUser> lstMember = new ArrayList<VSysUser>();
		// 如果为超级管理员 显示所有的员工信息
		if (sessionSysUser.getUserID() == 0) {
			lstMember = vSysUserRepository.findMemberSearchALL(strUserName, strUserSex, strUserMobile, startIndex,
					count);
		} else {
			// 如果非超级管理员
			lstMember = vSysUserRepository.findMemberSearchByOrgID(iOrgID, strUserName, strUserSex, strUserMobile,
					startIndex, count);
		}
		return lstMember;
	}

	/**
	 * 后台旅行社信息管理中显示自己公司的会员 超级管理员显示所有的会员 分页
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @date 创建日期 2020.11.19 修改时间2021.03.17
	 */
	public int queryMemberCount(TSysUser sessionSysUser, String strUserName, String strUserSex, String strUserMobile) {
		if (strUserName == null)
			strUserName = "";
		if (strUserSex == null)
			strUserSex = "";
		if (strUserMobile == null)
			strUserMobile = "";
		int iOrgID = sessionSysUser.getOrgID();
		int count = 0;
		if (sessionSysUser.getUserID() == 0) {
			count = vSysUserRepository.countMemberALL(strUserName, strUserSex, strUserMobile);
		} else {
			count = vSysUserRepository.countMemberByOrgID(iOrgID, strUserName, strUserSex, strUserMobile);
		}
		return count;
	}

	/**
	 * 后台创建会员 查询该公司所有的员工和会员
	 *
	 * @author 创建人 hanxl 整理 修改人hanxl
	 * @date 创建日期 2020.11.19 修改时间2021.03.17
	 */
	public List<TSysUser> findByOrgID(int iOrgID) {
		return tSysUserRepository.findByOrgID(iOrgID);
	}

	/**
	 * 创建会员 提交
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */
	public void createMember(TSysUser tSysUser, MultipartFile fCadeImg, MultipartFile fPhotoImg,
			TSysUser sessionSysUser) {
		/* 初始化游客账号登录名和密码 */
		tSysUser.setUserID(common.createBaseCode(UtilConstants.TableName.T_SYS_USER));
		StringBuffer memberAccount = new StringBuffer();
		memberAccount.append("tourist");
		memberAccount.append(tSysUser.getUserID());
		String strMemberAccount = memberAccount.toString();
		tSysUser.setUserAccount(strMemberAccount);
		// 设置初始密码为123abc
		tSysUser.setPassWord("123abc");
		// 证件照
		// 1.定义上传的文件
		String localPath = userPath;
		if (fCadeImg != null) {
			// 2.获得文件名字
			String cadeName = fCadeImg.getOriginalFilename();

			// 3.上传
			String newCadeName = FileNameUtils.getFileName(cadeName);

			// 3.1 生成新的文件名
			String realCadePath = localPath + "/" + newCadeName;

			// 3.2 保存文件
			File destPhoto = new File(realCadePath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto.getParentFile().exists()) {
				destPhoto.getParentFile().mkdir();
			}

			try {
				fCadeImg.transferTo(destPhoto);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setCadePath(newCadeName);
		}
		// 个人照片
		if (fPhotoImg != null) {
			// 2.获得文件名字
			String photoName = fPhotoImg.getOriginalFilename();

			// 3.上传
			String newPhotoName = FileNameUtils.getFileName(photoName);

			// 3.1 生成新的文件名
			String realPhotoPath = localPath + "/" + newPhotoName;

			// 3.2 保存文件
			File destPhoto2 = new File(realPhotoPath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto2.getParentFile().exists()) {
				destPhoto2.getParentFile().mkdir();
			}

			try {
				fPhotoImg.transferTo(destPhoto2);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setPhotoPath(newPhotoName);
		}

		tSysUser.setCreateDate(new Date());
		tSysUser.setCreator(sessionSysUser.getUserName());

		/* 注册 */
		tSysUserRepository.save(tSysUser.getUserID());
		tSysUserRepository.save(tSysUser);

	}

	/**
	 * 修改会员提交
	 * 
	 * @author 创建人 hanxl
	 * @date 创建日期 2021.03.17
	 */

	public void updateMember(TSysUser tSysUser, MultipartFile fCadeImg, MultipartFile fPhotoImg,
			TSysUser sessionSysUser) {
		/* 获取修改游客的ID */
		/* 定义一个新的实体类对象 */
		TSysUser dbSysUser = tSysUserRepository.findByID(tSysUser.getUserID());
		dbSysUser.setUserName(tSysUser.getUserName());
		dbSysUser.setSex(tSysUser.getSex());
		dbSysUser.setIdentity(tSysUser.getIdentity());
		dbSysUser.setUserMobile(tSysUser.getUserMobile());
		dbSysUser.setUserType(tSysUser.getUserType());
		dbSysUser.setOrgID(tSysUser.getOrgID());
		dbSysUser.setUpUserID(tSysUser.getUpUserID());
		dbSysUser.setMail(tSysUser.getMail());
		dbSysUser.setTel(tSysUser.getTel());
		dbSysUser.setFax(tSysUser.getFax());
		dbSysUser.setJob(tSysUser.getJob());

		// 1.定义上传的文件
		String localPath = userPath;
		if (fCadeImg != null) {
			// 2.获得文件名字
			String cadeName = fCadeImg.getOriginalFilename();

			// 3.上传
			String newCadeName = FileNameUtils.getFileName(cadeName);

			// 3.1 生成新的文件名
			String realCadePath = localPath + "/" + newCadeName;

			// 3.2 保存文件
			File destPhoto = new File(realCadePath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto.getParentFile().exists()) {
				destPhoto.getParentFile().mkdir();
			}

			try {
				fCadeImg.transferTo(destPhoto);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setCadePath(newCadeName);
		}
		// 个人照片
		if (fPhotoImg != null) {
			// 2.获得文件名字
			String photoName = fPhotoImg.getOriginalFilename();

			// 3.上传
			String newPhotoName = FileNameUtils.getFileName(photoName);

			// 3.1 生成新的文件名
			String realPhotoPath = localPath + "/" + newPhotoName;

			// 3.2 保存文件
			File destPhoto2 = new File(realPhotoPath);

			// 判断文件目目录是否存在,不存在则新建
			if (!destPhoto2.getParentFile().exists()) {
				destPhoto2.getParentFile().mkdir();
			}

			try {
				fPhotoImg.transferTo(destPhoto2);

			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			tSysUser.setPhotoPath(newPhotoName);
		}

		dbSysUser.setJob(tSysUser.getJob());

		dbSysUser.setVIP(tSysUser.getVIP());
		dbSysUser.setBlack(tSysUser.getBlack());
		dbSysUser.setAccountState(tSysUser.getAccountState());
		dbSysUser.setMemberState(tSysUser.getMemberState());
		dbSysUser.setMemo(tSysUser.getMemo());
		dbSysUser.setModifyDate(new Date());

		dbSysUser.setReviser(sessionSysUser.getUserName());

		tSysUserRepository.save(dbSysUser);

	}

	
	public List<Map<String,Object>> getToursByOrg(int iOrgID){
		
		return userDao.getToursByOrg(iOrgID);
	}
 
}
